//
//  PaddingLabel.swift
//  ezyform
//
//  Created by Moennig Q on 9/2/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import RxSwift
class PaddingLabel : BaseLabel {
    private lazy var disposeBag = DisposeBag()
    
    var padding = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
    
    @IBInspectable
    var paddingLeft: CGFloat {
        get { return padding.left }
        set { padding.left = newValue }
    }
    
    @IBInspectable
    var paddingRight: CGFloat {
        get { return padding.right }
        set { padding.right = newValue }
    }
    
    @IBInspectable
    var paddingTop: CGFloat {
        get { return padding.top }
        set { padding.top = newValue }
    }
    
    @IBInspectable
    var paddingBottom: CGFloat {
        get { return padding.bottom }
        set { padding.bottom = newValue }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //监听字体更改，重回四周的padding
//        Constant.standardViewFlagSuject.subscribe {[weak self] _ in
//            guard let strongSelf = self else {return }
//            strongSelf.drawText(in: strongSelf.frame)
//        }.disposed(by: self.disposeBag)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
    }
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let insets = self.padding
        var rect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        rect.origin.x    -= insets.left
        rect.origin.y    -= insets.top
        rect.size.width  += (insets.left + insets.right)
        rect.size.height += (insets.top + insets.bottom)
        return rect
    }
}
